﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2022 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers;

[AuthorityController(area: "HtAdmin", controllerDescription: "用户", PCode = "HtAdmin_User")]
public class UserController : HtAdminBaseController
{
    private readonly ILogger<UserController> _logger;
    private readonly IUserService _userService;
    private readonly HtRoleService _roleService;
    private readonly IHtAccountService _accountService;

    public UserController(ILogger<UserController> logger
      , IUserService userService
      , HtRoleService roleService
      , IHtAccountService accountService)
    {
        _logger = logger;
        _userService = userService;
        _roleService = roleService;
        _accountService = accountService;
    }

    [AuthorityMenu(name: "用户列表", CreateDate = "2021-07-31")]
    [AuthorityPermission(name: "用户列表", actions: "Index,GetList")]
    [HttpGet]
    public async Task<IActionResult> Index()
    {
        ViewBag.LoginUser = await _accountService.GetLoginUser();
        return View();
    }

    [UnifyResult(typeof(HtUserOut))]
    [HttpPost, HttpGet]
    public async Task<JsonResult> GetList(HtUserQuery searchquery)
    {
        var pagedlist = await _userService.GetAllByPageToLayui(searchquery);
        return Json(pagedlist);
    }

    [AuthorityPermission(name: "编辑", actions: "Info")]
    public async Task<ActionResult> Info(int? id)
    {
        ViewBag.LoginUser = await _accountService.GetLoginUser();
        ViewBag.Roles = (await _roleService.GetAll()).Where(o => o.IsEnable).ToList();
        HtUserOut entity = null;
        if (id.HasValue)
        {
            entity = await _userService.Get(id.Value);
        }
        return View(entity ?? new HtUserOut());
    }

    public async Task<IActionResult> Add()
    {
        ViewBag.Roles = (await _roleService.GetAll()).Where(o => o.IsEnable).ToList();
        ViewBag.LoginUser = await _accountService.GetLoginUser();

        return View("Info", new HtUserOut() { IsEnable = true });
    }

    [HttpPost]
    public async Task<JsonResult> Edit(HtUserInput input)
    {
        await _userService.InsertOrUpdate(input);
        return Json("");
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [AuthorityPermission(name: "删除", actions: "Delete")]
    [HttpPost]
    public async Task<ActionResult> Delete(int ids)
    {
        await _userService.Delete(ids);
        return Json("");
    }

    /// <summary>
    /// 批量删除
    /// </summary>
    /// <param name="ids">需要删除数据，多个英文逗号隔开</param>
    /// <returns></returns>
    [HttpPost]
    [AuthorityPermission(name: "批量删除", actions: "BatchDelete")]
    public async Task<ActionResult> BatchDelete(string ids)
    {
        await _userService.BatchDelete(ids);
        return Json("");
    }

    [AuthorityPermission(name: "设置状态", actions: "SetState")]
    [HttpPost]
    public async Task<JsonResult> SetState(int id, bool state, string type)
    {
        await _userService.SetState(id, state, type);
        return Json("");
    }
}